/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kweative.model.apt;

import java.util.HashSet;
import java.util.Stack;

/**
 *
 * @author henrik
 */
public class ClassDefinition {
    private String packageName;
    private String className;
    private String superClass;
    private Stack<String> interfaces = new Stack<String>();

    private Stack<String> imports = new Stack<String>();
    private Stack<VariableDefinition> fields = new Stack<VariableDefinition>();
    private Stack<MethodDefinition> methods = new Stack<MethodDefinition>();

    public ClassDefinition(String packageName, String className) {
        this.packageName = packageName;
        this.className = className;
    }
    public String getFQN() {
        return String.format("%s.%s",packageName,className);
    }
    public void setSuperClass(String superClass) {
        this.superClass = superClass;
    }
    
    public void addInterface(String name) {
        interfaces.add(name);
        addImport(name);
    }

    public void addField(String type,String name) {
        fields.push(new VariableDefinition(type, name));
        addImport(type);
    }
    public MethodDefinition addMethod(String name,String returnType) {
        MethodDefinition method = new MethodDefinition(this, name, returnType);
        methods.push(method);
        addImport(returnType);
        return method;
    }
    public void addImport(String className) {
        imports.push(className);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("package ").append(packageName);
        sb.append(";\n\npublic class ").append(className);
        if (superClass != null) {
            sb.append(" extends ").append(superClass);
        }
        if (interfaces.size() > 0) {
            sb.append(" implements");
            boolean first = true;
            for(String iface:interfaces) {
                if (!first)
                    sb.append(",");
                sb.append(iface);
                first = false;
            }
        }
        sb.append(" {\n");

        for(VariableDefinition field: fields) {
            sb.append("\t")
                .append("public ")
                .append(field.toString())
                .append(";\n");
        }
        for(MethodDefinition method: methods) {
            sb.append("\t")
                .append(method.toString())
                .append("\n");
        }

        sb.append("}");
        return sb.toString();
    }

}
